package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemDBHelper;
import id.ac.amikom.libs.AdemServiceHelper;
import id.ac.amikom.libs.SpecialAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class AdemInfoActivity extends ListActivity {

	private AdemDBHelper db = null;
	private ListAdapter adapter;

	private class GetDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			AdemServiceHelper srv = new AdemServiceHelper(AdemInfoActivity.this);
			if (srv.checkInfoService())
				srv.getInfoService();

			return null;

		}

		@Override
		protected void onPostExecute(Void result) {
			setList();

			((PullToRefreshListView) getListView()).onRefreshComplete();
			super.onPostExecute(result);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);				
		
		setContentView(R.layout.adem_list);

		((PullToRefreshListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {
					public void onRefresh() {
						new GetDataTask().execute();
					}
				});

		db = new AdemDBHelper(this);
		setList();

		

	}

	public void setList() {
		try {

			Cursor cr = db.getInfo();
			startManagingCursor(cr);

			ArrayList<HashMap<String, String>> infoList = new ArrayList<HashMap<String, String>>();

			if (cr.moveToFirst()) {
				do {

					String[] date = cr.getString(3).trim().split("\\s+");

					HashMap<String, String> map = new HashMap<String, String>();
					map.put("title", cr.getString(2));
					map.put("pub_id", cr.getString(1));
					map.put("date",
							date[1] + " " + date[0] + " " + date[2] + " - "
									+ date[3].substring(0, 5)
									+ date[3].substring(12, 14));
					infoList.add(map);
				} while (cr.moveToNext());
			}

			adapter = new SpecialAdapter(this, infoList, R.layout.adem_list,
					new String[] { "title", "date" }, new int[] {
							R.id.list_title, R.id.list_date });

			setListAdapter(adapter);
			registerForContextMenu(getListView());
			db.close();

		} catch (Exception e) {
			Log.e("Exception found ", e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.id_login:
			// new GetDataTask().execute();
			startActivity(new Intent(AdemInfoActivity.this,
					AdemLoginActivity.class));
			finish();

			return true;
		case R.id.id_setting:
			startActivity(new Intent(AdemInfoActivity.this,
					AdemSettingActivity.class));
			finish();

			return true;
		case R.id.id_about:
			Dialog dialog = new Dialog(this);
			dialog.setContentView(R.layout.adem_info);
			dialog.setTitle("Amikom Social");
			dialog.setCancelable(true);
			dialog.show();

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@SuppressWarnings("rawtypes")
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Object obj = this.getListAdapter().getItem(position - 1);
		HashMap fob = (HashMap) obj;
		String _id = (String) fob.get("pub_id");

		Intent in = new Intent(AdemInfoActivity.this, AdemDetailActivity.class);
		in.putExtra("pid", _id.trim());
		in.putExtra("type", "info");
		startActivity(in);

	}

	@Override
	public void startManagingCursor(Cursor c) {

		if (Build.VERSION.SDK_INT < 11) {
			super.startManagingCursor(c);
		}
	}

}